package backend;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.NodeList;

public class DvdInformation implements ProductInformation {
	private int product_id;
	private String actors;
	private String format;
	private String language;
	private String subtitles;
	private String region;
	private String aspect_ratio;
	private int number_discs;
	private Date release_date;
	private double run_time;
	private String asin;

	public DvdInformation(int product_id, String actors, String format,
			String language, String subtitles, String region,
			String aspect_ratio, int number_discs, Date release_date,
			double run_time, String asin) {
		super();
		this.product_id = product_id;
		this.actors = actors;
		this.format = format;
		this.language = language;
		this.subtitles = subtitles;
		this.region = region;
		this.aspect_ratio = aspect_ratio;
		this.number_discs = number_discs;
		this.release_date = release_date;
		this.run_time = run_time;
		this.asin = asin;
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see java.lang.Object#hashCode()
	 */
	@Override
	public int hashCode() {
		final int prime = 31;
		int result = 1;
		result = prime * result + product_id;
		return result;
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see java.lang.Object#equals(java.lang.Object)
	 */
	@Override
	public boolean equals(Object obj) {
		if (this == obj)
			return true;
		if (obj == null)
			return false;
		if (!(obj instanceof DvdInformation))
			return false;
		DvdInformation other = (DvdInformation) obj;
		if (product_id != other.product_id)
			return false;
		return true;
	}

	@Override
	public List<String> getValues() {
		List<String> resp = new ArrayList<String>();
		resp.add(new Integer(product_id).toString());
		resp.add(actors);
		resp.add(format);
		resp.add(language);
		resp.add(subtitles);
		resp.add(region);
		resp.add(aspect_ratio);
		resp.add(new Double(number_discs).toString());
		resp.add(release_date.toString());
		resp.add(new Double(run_time).toString());
		resp.add(asin);
		return resp;

	}

	public DvdInformation(int id) throws Exception {
		super();
		this.product_id=id;
		Connection.setServiceUrl("http://eiffel.itba.edu.ar/hci/service/Catalog.groovy");
		String[] arg = { "product_id=" +id };
		Document dom = Connection.connect("GetProduct", "GET", arg);
		NodeList nodes = dom.getElementsByTagName("product");
		Element e = (Element) nodes.item(0);
		this.actors = ((e.getElementsByTagName("actors")).item(0))
				.getTextContent();
		this.format = ((e.getElementsByTagName("format")).item(0))
				.getTextContent();
		this.language = ((e.getElementsByTagName("language")).item(0))
				.getTextContent();
		this.subtitles = ((e.getElementsByTagName("subtitles")).item(0))
				.getTextContent();
		this.region = ((e.getElementsByTagName("region")).item(0))
				.getTextContent();
		this.aspect_ratio = ((e.getElementsByTagName("aspect_ratio"))
				.item(0)).getTextContent();
		this.number_discs = (new Integer(((e
				.getElementsByTagName("number_discs")).item(0))
				.getTextContent())).intValue();
		;
		SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");
		String strFecha = ((e.getElementsByTagName("release_date")).item(0))
				.getTextContent();
		this.release_date = null;
		
		/*Esto puede tirar una ParseException*/
		release_date = dateFormat.parse(strFecha);
		
		this.run_time = (new Double(((e.getElementsByTagName("run_time"))
				.item(0)).getTextContent())).doubleValue();
		this.asin = ((e.getElementsByTagName("ASIN")).item(0))
				.getTextContent();
	}

}
